package com.qc31.monitorlib.ui.carmonitor

import autodispose2.autoDispose
import com.qc31.baselib.utils.GsonUtil
import com.qc31.comlib.TimeUtil
import com.qc31.comlib.UrlUtil
import com.qc31.comlib.entity.HistoryVideo
import com.qc31.comlib.entity.KeyValue
import com.qc31.comlib.entity.VideosPass
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.R
import com.qc31.monitorlib.common.ChooseTimeViewModel
import io.reactivex.rxjava3.functions.Consumer
import io.reactivex.rxjava3.subjects.PublishSubject

/** @Author Jasper @Time 2020/9/19 @Desc 车辆历史 */
class CarHistoryVideoViewModel : ChooseTimeViewModel() {
    //是否查询终端视频
    var isTmn = true
    var carId = ""
    var carName = ""
    var videos = ""
        set(value) {
            handleVideos(value)
        }

    //通道下标
    var tmnPass: Int = 0

    //通道
    var pass: String = "0"

    //音视频资源下标
    var tmnRes: Int = 0

    //码流下标
    var tmnStream: Int = 0

    //存储器下标
    var tmnStorage: Int = 0

    //弹窗类型 0.通道 1.视频 2.码流 3.存储器
    var popupType = 0


    //视频通道
    val videoPassList = ArrayList<KeyValue>()

    val entitySub: PublishSubject<MutableList<HistoryVideo.Result>>
            by lazy { PublishSubject.create() }
    private var allPass = ""
    private var passName = ""

    fun init(allPass: String, passName: String) {
        this.allPass = allPass
        this.passName = passName
        videoPassList.clear()
        videoPassList.add(KeyValue("0", allPass))
    }

    /** 视频通道处理 */
    private fun handleVideos(videos: String) {
        videoPassList.clear()
        videoPassList.add(KeyValue("0", allPass))
        if (videos.isNotEmpty()) {
            val entity = GsonUtil.formObject<VideosPass>(videos)
            val passEntitys = entity.vs.split(",")
            for (pass in passEntitys) {
                val passInfos = pass.split(":")
                val passNum = passInfos[0]
                if (passInfos[1].isNotEmpty()) {
                    videoPassList.add(KeyValue(passNum, passInfos[1]))
                } else {
                    videoPassList.add(
                        KeyValue(
                            passNum,
                            "$passName $passNum"
                        )
                    )
                }
            }
        }
    }

    /** 获取视频列表 */
    fun getTmVideosInfo(startTime: String, endTime: String) {
        if (carId.isEmpty()) {
            toast(R.string.hint_toast_choose_car)
            return
        }
        val start = TimeUtil.instance.formatHms(startTime).substring(2)
        val end = TimeUtil.instance.formatEndHms(endTime).substring(2)
        if (isTimeOk(startTime, endTime)) return
        toastLoading(true)
        ServiceHelper.getRepository().getVideoState(getUrl(start, end))
            .autoDispose(this)
            .subscribe(Consumer {
                toastLoading(false)

                val videoEntity =
                    GsonUtil.formObject<HistoryVideo>(it)
                if (videoEntity.state != "0") {
                    toast(UrlUtil.getToastMap()[videoEntity.state] ?: "")
                    return@Consumer
                }
                if (videoEntity.result.isNullOrEmpty()) {
                    toast(R.string.desc_no_about_data)
                    return@Consumer
                }
                entitySub.onNext(videoEntity.result)

            }, error)
    }

    private fun getUrl(startTime: String, endTime: String): String {
        return if (isTmn) {
            "httpflv.hquery.$carId.$pass.$startTime.$endTime.$tmnRes.$tmnStream.$tmnStorage"
        } else {
            "httpflv.squery.$carId.$pass.$startTime.$endTime"
        }
    }


}
